<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="账户"
          label="账户"
          placeholder="用户名"
          :rules="[{ required: true, message: '请输入账号' }, { min: 6, message: '账号至少6位', trigger: 'onBlur' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请输入密码' }, { min: 6, message: '密码至少6位', trigger: 'onBlur' }]"
      />
      <van-field
          v-model="checkPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请确认密码' }, { validator: validateConfirmPassword, trigger: 'onBlur' }]"
      />
      <van-field
          v-model="planetCode"
          type="text"
          name="星球编号"
          label="星球编号"
          placeholder="星球编号"
          :rules="[{ required: true, message: '请输入星球编号' },  { min: 1,max:5, message: '密码至少6位', trigger: 'onBlur' }]"
      />
    </van-cell-group>
    <div style="margin: 16px;">
      <van-button round block type="primary" native-type="submit">
        提交
      </van-button>
    </div>
  </van-form>

</template>
<script setup>
import {computed, ref} from "vue";
import myAxios from "../plugins/myAxios.ts";
import {showFailToast, showSuccessToast} from "vant";
import {useRoute, useRouter} from "vue-router";

const router = useRouter()
const route = useRoute()
const userAccount = ref('');
const userPassword = ref('');
const checkPassword = ref('');
const planetCode = ref('')
const onSubmit = async () => {
  const res = await myAxios.post('/userInfo/register', {
    userAccount: userAccount.value,
    userPassword: userPassword.value,
    checkPassword: checkPassword.value,
    planetCode: planetCode.value
  })
  console.log(res.data)
  if (res.code === 0 && res.data != null) {
    showSuccessToast('注册成功')
    router.replace('/login')
  } else {
    showFailToast(res.description)
  }
}
const validateConfirmPassword = (value) => {
  if (value !== checkPassword.value) {
    return new Error('两次输入的密码不一致');
  }
  return true;
};
</script>
<style scoped>

</style>